/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is a Test User Interface meant for demo purposes.
 */
var host = null;

function redirect(href) {
	window.location = host+'/'+href;
}

function popup(target,callback) {
	var target_id = target.substring(0,target.indexOf('.'));
	$('#fade').attr('class','block');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/'+target,  
		beforeSend: function() {
			$('.content').append("<div id='"+target_id+"' class='popup'><div class='popup-content'><div class='loader'></div></div></div>");
		},
		success: function(data) {
			$('#'+target_id+' .popup-content').html(data);
			if($('form').find('input')) {
				$('form').find('input:first').focus();
				$('form').find('input[tabindex^=1]').focus();
			}
			if(callback) {
				callback();
			}
		},
		error: function() {
			$('#'+target_id+' .popup-content').html('<p class="center">Sorry, the Ajax call could not be completed. Please try again later.');
		}
	}); 
}

function closePopup() {
	if($('#fade').attr('class','block')) {
		$('#fade').attr('class','invisible');
	}
	if($('.popup')) {
		$('.popup').remove();
	}
}

function expandCourse(target,callback) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.expand').attr('href','javascript: collapseCourse("course-'+id+'")').attr('class','collapse');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/expandcourse.php?c='+id,
		beforeSend: function() {
			$(target).append("<div class='loader'></div>");
		},
		success: function(data) {
			$(target).find('div.loader').remove();
			$(target).append(data);
			if($(target).find('.annotation-href').html() != 'annotate') {
				var splits = target.split('-');
				showAnnotations(splits[1]);
			}
			window.location.hash = target;
			if(callback) {
				callback();
			}
		},
		error: function() {
			$(target).find('div.loader').remove();
			$(target).append('<div class="course-info"><p>Sorry, the Ajax call could not be completed. Please try again later.</p></div>');
		},
	});
}

function collapseCourse(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.collapse').attr('href','javascript: expandCourse("course-'+id+'")').attr('class','expand');
	$(target).find('div.course-info').remove();
	$(target).find('.course-annotations').remove();
	window.location.hash = '';
}

function expandGroup(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.expand').attr('href','javascript: collapseGroup("group-'+id+'")').attr('class','collapse');
	$.ajax({
		type: "GET",  
		url: host+'/ajax/expandgroup.php?g='+id,
		beforeSend: function() {
			$(target).append("<div class='loader'></div>");
		},
		success: function(data) {
			$(target).find('div.loader').remove();
			$(target).append(data);
			window.location.hash = window.location.hash.split('#').splice(1,1) + target;
		},
		error: function() {
			$(target).find('div.loader').remove();
			$(target).append('<div class="students"><p>Sorry, the Ajax call could not be completed. Please try again later.</p></div>');
		}
	});
}

function collapseGroup(target) {
	target = '#'+target;
	var id = target.substring(target.indexOf('-')+1);
	$(target).find('a.collapse').attr('href','javascript: expandGroup("group-'+id+'")').attr('class','expand');
	$(target).find('div.group-info').remove();
	window.location.hash = window.location.hash.split('#').splice(1,1);
	
}

function updateGroupFeeds(group_id) {
	var target = $('#update-'+group_id);
	var timer = null;
	$.ajax({
		type: "GET",  
		url: host+'/scheduledparser.php?g='+group_id,
		beforeSend: function() {
			$(target).html('updating.');
			var count = 0;
			timer = setInterval(function(){count++;if(count <= 2) {$(target).append('.');} else {count = 0;$(target).html('updating.');}},1000);
		},
		success: function(data) {
			clearInterval(timer);
			if(data) {
				if($(target)) {
					var x = $.parseJSON(data);
					if(x.totals == 0) {
						$(target).html('feeds are up-to-date, refresh');
					} else {
						$(target).html('updates found... refreshing page.');
						window.location.hash = window.location.hash.split('#').splice(1,1) + '#group-' + group_id;
						setTimeout(window.location.reload(),1000);
					}
				}
			} else {
				$(target).html('error, try again.');
			}
		},
		error: function() {
			$(target).html('error, try again.');
		}
	});	
}

function showAnnotations(course_id) {
	if($('#course-'+course_id).find('.course-info').html() == null) {
		expandCourse('course-'+course_id);
	}
	$.ajax({
		type: "GET",  
		url: host+'/ajax/annotatecourse.php?c='+course_id,  
		beforeSend: function() {
			$('#course-'+course_id).find('.course-annotations').html('<div class="loader"></div>');
		},
		success: function(data) {
			$('#course-'+course_id).find('.course-annotations').html(data);
		},
		error: function() {
			$('#course-'+course_id).find('.course-annotations').html('<p class="center">Sorry, the Ajax call could not be completed. Please try again later.');
		}
	}); 
}

$(document).ready(function() {
	host = $('#host-name').html();
	
	if($('form').find('input')) {
		$('form').find('input:first').focus();
	}
	if(window.location.hash != '') {
		var h = window.location.hash.split('#');
		if(h[1]) {
			if(h[2]) {
				expandCourse(h[1],function() {
					var link = $('#'+h[2]).find('a.expand');
					link.attr('onclick',link.attr('href'));
					link.trigger('click');
					$('#'+h[2]).find('a.collapse').removeAttr('onclick');
				});
			} else {
				expandCourse(h[1]);
			}
		}
	}
});